package com.carkeeper.servcieManagement.service.impl;

import com.carkeeper.servcieManagement.dao.ServiceMapper;
import com.carkeeper.servcieManagement.service.ServiceServcie;
import com.carkeeper.serviceManagement.pojo.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengjinxiong
 * @version 1.0
 * @date 2020/11/24 13:55
 */
@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceServcie {

    @Autowired
    private ServiceMapper serviceMapper;

    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Service> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        List<Service> serviceList= serviceMapper.selectByExample(example);
        System.out.println(serviceList);
        return serviceList;
    }

    @Override
    public Service findServiceByCode(String code) {
        Map<String, Object> searchMap = new HashMap<>();
        searchMap.put("code", code);
        Example example = createExample(searchMap);
        Service service = serviceMapper.selectOneByExample(example);
        return service;

    }
 //上下架
    @Override
    public void put(Long id, String status) {
        Service service = serviceMapper.selectByPrimaryKey(id);
        if (service == null) {
            throw new RuntimeException("此服务不存在");
        }
        service.setStatus(status);
        serviceMapper.updateByPrimaryKeySelective(service);

    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Service.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 服务id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 商品状态 1-正常，2-下架，3-删除
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
        }
        return example;
    }
}
